package Controller

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"hr-system/Global"
	"hr-system/Model"
	response "hr-system/Response"
	"time"
)

type INewEmployeeController interface {
	RestController
	Update(c *gin.Context)
}

type NewEmployeeController struct {
	DB *gorm.DB
}

func NewNewEmployeeController() INewEmployeeController {
	db := Global.GetDB()
	return NewEmployeeController{DB: db}
}


func (n NewEmployeeController) PageList(c *gin.Context) {
	var requestEmployee Model.Employee
	_ = c.ShouldBind(&requestEmployee)
	name := requestEmployee.Name
	if name != "" {
		var selectEmployee Model.Employee
		// 查询
		result := n.DB.Model(&selectEmployee).Where("name=? and is_quit='否'", name).First(&selectEmployee).RowsAffected
		if result == 0 {
			response.Fail(c, "查无此员工", nil)
			return
		}
	}
	var record []Model.Employee
	n.DB.Order("id").Where("is_quit='否'").Find(&record)
	length := len(record)
	newRecord := make([]Model.NewEmployee, 0)
	for i:=0; i<length; i++ {
		if name!="" && name!= record[i].Name {
			continue
		}
		var tempD Model.Department
		var tempP Model.Post
		n.DB.Where("department_number=?",record[i].DepartmentNumber).First(&tempD)
		n.DB.Where("post_number=?",record[i].PostNumber).First(&tempP)
		var start string
		var end string
		if record[i].IsProbation == "否" {
			start = "-"
			end = "-"
		} else {
			start = record[i].ProbationStart.Format("2006-01-02")
			end = record[i].ProbationEnd.Format("2006-01-02")
		}
		newRecord = append(newRecord, Model.NewEmployee{
			UniEmployee: record[i].UniEmployee,
			Birthday:record[i].Birthday.Format("2006-01-02"),
			DepartmentName:tempD.DepartmentName,
			PostName:tempP.PostName,
			EntryDate:record[i].EntryDate.Format("2006-01-02"),
			GraduationDate:record[i].GraduationDate.Format("2006-01-02"),
			ProbationStart:start,
			ProbationEnd:end,
		})
	}
	response.Success(c, gin.H{"data": newRecord}, "查询成功")
}

func (n NewEmployeeController) Create(c *gin.Context) {
	var requestEmployee Model.ReturnEmployee
	_ = c.ShouldBind(&requestEmployee)
	var updateEmployee Model.Employee
	result := n.DB.Where("id=?", requestEmployee.Id).Find(&updateEmployee).RowsAffected
	if result != 0 {
		response.Fail(c, "该员工已存在", nil)
		return
	}
	var tempD Model.Department
	var tempP Model.Post
	n.DB.Where("department_name=?",requestEmployee.DepartmentName).First(&tempD)
	n.DB.Where("post_name=?",requestEmployee.PostName).First(&tempP)
	var start time.Time
	var end time.Time
	if requestEmployee.IsProbation == "是" {
		start,_ = time.Parse("2006-01-02", requestEmployee.ProbationStart)
		end,_ = time.Parse("2006-01-02", requestEmployee.ProbationEnd)
	}
	birth,_ := time.Parse("2006-01-02", requestEmployee.Birthday)
	entry,_ := time.Parse("2006-01-02", requestEmployee.EntryDate)
	graduation, _ := time.Parse("2006-01-02", requestEmployee.GraduationDate)
 	newEmployee := Model.Employee{
 		UniEmployee: requestEmployee.UniEmployee,
		Birthday:birth,
		DepartmentNumber:tempD.DepartmentNumber,
		PostNumber:tempP.PostNumber,
		EntryDate:entry,
		GraduationDate:graduation,
		ProbationStart:start,
		ProbationEnd:end,
		IsQuit: "否",
	}
	err := n.DB.Create(&newEmployee).Error
	if err != nil {
		response.Fail(c, err.Error(), nil)
		return
	}
	response.Success(c, gin.H{"data": requestEmployee}, "添加成功")
}

func (n NewEmployeeController) Update(c *gin.Context) {
	// 绑定body中参数（更新后的值）
	var requestEmployee Model.ReturnEmployee
	_ = c.ShouldBind(&requestEmployee)
	// 获取path中的参数（要更新的值）
	EmployeeId := requestEmployee.Id
	var originalEmployee Model.Employee
	n.DB.Where("id=?", EmployeeId).First(&originalEmployee)
	// 更新
	var tempD Model.Department
	var tempP Model.Post
	n.DB.Where("department_name=?",requestEmployee.DepartmentName).First(&tempD)
	n.DB.Where("post_name=?",requestEmployee.PostName).First(&tempP)
	start := originalEmployee.ProbationStart
	end := originalEmployee.ProbationEnd
	if requestEmployee.IsProbation == "是" {
		start,_ = time.Parse("2006-01-02", requestEmployee.ProbationStart)
		end,_ = time.Parse("2006-01-02", requestEmployee.ProbationEnd)
	}
	birth,_ := time.Parse("2006-01-02", requestEmployee.Birthday)
	entry,_ := time.Parse("2006-01-02", requestEmployee.EntryDate)
	graduation, _ := time.Parse("2006-01-02", requestEmployee.GraduationDate)
	updateEmployee := Model.Employee {
		UniEmployee: requestEmployee.UniEmployee,
		Birthday:birth,
		DepartmentNumber:tempD.DepartmentNumber,
		PostNumber:tempP.PostNumber,
		EntryDate:entry,
		GraduationDate:graduation,
		ProbationStart:start,
		ProbationEnd:end,
		IsQuit: "否",
	}
	err := n.DB.Model(&updateEmployee).Where("id=?", EmployeeId).Updates(&updateEmployee).Error
	if err != nil {
		response.Fail(c, err.Error(), nil)
		return
	}
	response.Success(c, gin.H{"data": requestEmployee}, "修改成功")
}
